package com.ts.opengl.simpleDraw.TransformFeedback

import android.content.Context
import android.opengl.GLES20.GL_NONE
import android.opengl.GLES20.GL_TRIANGLE_STRIP
import android.opengl.GLES20.GL_UNSIGNED_INT
import android.opengl.GLES20.glBindBuffer
import android.opengl.GLES20.glBufferData
import android.opengl.GLES20.glGenBuffers
import android.opengl.GLES20.glGetError
import android.opengl.GLES30
import android.opengl.GLES30.GL_MAP_READ_BIT
import android.opengl.GLES30.GL_STATIC_READ
import android.opengl.GLES30.GL_TRANSFORM_FEEDBACK
import android.opengl.GLES30.GL_TRANSFORM_FEEDBACK_BUFFER
import android.opengl.GLES30.glBeginTransformFeedback
import android.opengl.GLES30.glBindBufferBase
import android.opengl.GLES30.glBindTransformFeedback
import android.opengl.GLES30.glEndTransformFeedback
import android.opengl.GLES30.glGenTransformFeedbacks
import android.opengl.GLES30.glMapBufferRange
import android.util.Log
import com.ts.audiocamera.R
import com.ts.opengl.simpleDraw.BaseRender
import javax.microedition.khronos.opengles.GL10

class transformeFeedback(conext: Context) : BaseRender(conext) {
    override fun getFragmentId(): Int =R.raw.fbo_new_frag

    override fun getVertex(): Int = R.raw.fbo_new_ver


    var mTransFeedbackObjId=IntArray(1)
    var transFeedbackBufId=IntArray(1)
    override fun initBufferNew(){


        glGenBuffers(transFeedbackBufId.size,transFeedbackBufId,0)
        glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER,transFeedbackBufId[0])
        glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,(3 + 2) * 6 * 4,null,GL_STATIC_READ)



        //创建vbo对象
        glGenTransformFeedbacks(mTransFeedbackObjId.size,mTransFeedbackObjId,0)
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK,mTransFeedbackObjId[0])
        glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, transFeedbackBufId[0])
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK,0)
        glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER,0)



    }

    private  val TAG = "transformeFeedback"
    override fun onDrawFrame(gl: GL10?) {
        GLES30.glUseProgram(program)


        GLES30.glBindVertexArray(vao[0])
//        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,textures[0])
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,imagetextture[0])
        Log.e(TAG, glGetError().toString()+"")

        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, mTransFeedbackObjId[0]);
        glBeginTransformFeedback(GL_TRIANGLE_STRIP);
        GLES30.glDrawElements(GLES30.GL_TRIANGLE_STRIP,6, GL_UNSIGNED_INT,0)
        glEndTransformFeedback();
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);

        GLES30.glBindVertexArray(GL_NONE)
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, GL_NONE)

        glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, transFeedbackBufId[0]);

        var buffer=glMapBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER,0,(3 + 2) * 6 * 4,GL_MAP_READ_BIT)

        for (i in 0 until buffer.capacity()) {
            Log.e(TAG,buffer.position(i).toString()+"---")
        }
//        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedbacks[0]);

    }
}